﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Torch_Entity.Torch_SqlEntity;

namespace Torch_Repository
{

    /// <summary>
    ///     仓储层 父类
    /// </summary>
    /// <typeparam name="T">模型(Entity)层子类</typeparam>
    public class BaseDal<T> : IBaseDal<T> where T : class, new()
    {
        private SmartSchoolDbContext _dbcontext;
        public BaseDal(SmartSchoolDbContext dbcontext)
        {
            _dbcontext = dbcontext;
        }


        // 新增
        public async Task<int> AddAsync(T entity, bool isSaved = true)
        {
            try
            {
                _dbcontext.Entry(entity).State = EntityState.Added;

                if (isSaved)
                    return await _dbcontext.SaveChangesAsync();

                return 0;
            }
            catch
            {
                return -1;
            }

        }

        // 删除
        public virtual async Task<int> DeleteAsync(T entity, bool isSaved = true)
        {
            try
            {
                _dbcontext.Entry(entity).State = EntityState.Deleted;
                if (isSaved)
                    return await _dbcontext.SaveChangesAsync();
                return 0;
            }
            catch
            {
                return -1;
            }
        }

        // 批量删除
        // ------------------------------------------------以封印--------------------------------------------------------------------
        //public async Task<int> DeleteListAsync(params T[] modelList)
        //{
        //    var res = 0;

        //    using (IDbContextTransaction _dctran = _dbcontext.Database.BeginTransaction()) 
        //    {
        //        try
        //        {
        //            foreach (var item in modelList)
        //            {
        //                _dbcontext.Entry(item).State = EntityState.Deleted;
        //            }
        //            res = await _dbcontext.SaveChangesAsync();
        //            if (res < modelList.Length) // 数据不对等 则执行回滚操作
        //            {
        //                _dctran.Rollback();

        //                res = 0;
        //            }
        //            else
        //            {
        //                _dctran.Commit();
        //            }
        //        }
        //        catch (Exception)
        //        {
        //            _dctran.Rollback();
        //            res = 0;
        //        }
        //        return res;
        //    }
        //}

        // 修改

        public async Task<int> EditAsync(T entity, bool isSaved = true)
        {
            try
            {
                _dbcontext.Entry(entity).State = EntityState.Modified;
                if (isSaved)
                    return await _dbcontext.SaveChangesAsync();
                return 0;
            }
            catch
            {
                return -1;
            }
        }

        public async Task<List<T>> QueryAsync()
        {
            try
            {
                return await _dbcontext.Set<T>().ToListAsync();
            }
            catch
            {
                return null;
            }
        }

        public async Task<List<T>> QueryAsync(Expression<Func<T, bool>> whereLambda)
        {
            try
            {
                return await _dbcontext.Set<T>().Where(whereLambda).ToListAsync();
            }
            catch
            {
                return null;
            }
        }

        // 编号查询
        public async Task<T> QueryAsync(int id)
        {
            try
            {
                return await _dbcontext.Set<T>().FindAsync(id);
            }
            catch
            {
                return null;
            }
        }

        // 保存数据
        public async Task<int> SaveDataAsync()
        {
            try
            {
                return await _dbcontext.SaveChangesAsync();
            }
            catch
            {
                return -1;
            }
        }

    }
}
